package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.domain.basic.DictCategory;
import cc.yugu.yhdw.common.domain.basic.DictItem;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.entity.tree.Node;
import cc.yugu.yhdw.common.entity.tree.NodeModel;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.biz.client.ClientBizUtils;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.persistence.basic.DictCategoryMapper;
import cc.yugu.yhdw.webserver.persistence.basic.DictItemMapper;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
 * 字典业务
 * 
 * @Date:Created in 2018/7/3 16:25
 * @Author:
 * @Modified By: zjn 2018/7/3 16:25
 */ 
@Service
public class DictItemService {

    @Autowired
    DictItemMapper dictItemMapper;
    @Autowired
    DictCategoryMapper dictCategoryMapper;
    @Autowired
    AppConfig appConfig;
    /**
    * 根据主键查询表 bas_dict_item
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.basic.DictItem
    * @Author By: zjn 2018/7/3  16:25
    */ 
    public DictItem find(int id) {
        return dictItemMapper.find(id);
    }
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/3  16:31
    */ 
    public Page findPage(DictItem search) {
        Page page = search.buildPage();
        page.setTotalItems(dictItemMapper.findPageCount(search));
        search.setBeginIndex(page.getOffset());
        page.setResult(dictItemMapper.findPageResult(search));
        return page;
    }
    /**
    * 插入
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/3  16:31
    */ 
    public ExtResult insert(DictItem entity) {
        if(dictItemMapper.insert(entity) == 0) {
            return ExtResult.failResult("操作失败");
        }

        return ExtResult.successResult();
    }
    /**
    * 更新
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/3  16:31
    */ 
    public ExtResult update(DictItem entity) {
        if(dictItemMapper.update(entity) == 0) {
            return ExtResult.failResult("操作失败");
        }

        return ExtResult.successResult();
    }
    /**
    * 删除
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/3  16:32
    */ 
    public ExtResult delete(int id) {
        DictItem entity = find(id);
        if(dictItemMapper.delete(id) == 0) {
            return ExtResult.failResult("操作失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 输出
    * 
    * @Param: [stream]
    * @Return: void
    * @Author By: zjn 2018/7/3  16:32
    */ 
    public void dictCategoryJson(OutputStream stream) throws IOException {
        // 查询字典分类表bas_dict_category中所有信息
        List<DictCategory> list =  dictCategoryMapper.findAll();
        List<Node<NodeModel>> roots = new ArrayList<Node<NodeModel>>();
        for(DictCategory dictCategory : list) {
            NodeModel nodeModel = new NodeModel();
            Node<NodeModel> root = new Node<NodeModel>(nodeModel);
            nodeModel.setId(dictCategory.getId());
            nodeModel.setName(dictCategory.getCategoryName());
            roots.add(root);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        JsonGenerator json = objectMapper.getJsonFactory().createJsonGenerator(stream, JsonEncoding.UTF8);
        json.writeStartArray();
        YhdwUtils.writeJson(roots, json);
        json.writeEndArray();
        json.flush();
        json.close();
    }
}
